package com.example.q_kang.hhh.login;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.support.constraint.Group;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.util.CodeUtils;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPActivity;
import com.sskj.base.base.App;
import com.sskj.base.bean.AgreementBean;
import com.sskj.base.bean.RegisterBean;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/11
 */
public class RegisterActivity extends MVPActivity<RegisterPresenter> {


    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.rl_pwd_see)
    CheckBox rlPwdSee;
    @BindView(R.id.rl_pay_pwd_see)
    CheckBox rlPayPwdSee;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.iv_picture_code)
    ImageView ivPictureCode;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_mobile_code)
    EditText etMobileCode;
    @BindView(R.id.et_picture_code)
    EditText etPictureCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.cb_read)
    CheckBox cbRead;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.group_sms)
    Group group_sms;
    private CodeUtils codeUtils;
    private Bitmap bitmap;
    private String isShowSMS = "1";

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        codeUtils = CodeUtils.getInstance();
        bitmap = codeUtils.createBitmap();
        ivPictureCode.setImageBitmap(bitmap);
        ClickUtil.click(300, ivPictureCode, view -> {
            bitmap = codeUtils.createBitmap();
            ivPictureCode.setImageBitmap(bitmap);
        });
        ClickUtil.click(300, tvAgreement, view -> {
            AgreementActivity.start(this);

        });
        ClickUtil.click(300, tvGetCode, new ClickUtil.Click() {
            @Override
            public void click(View view) {
                if (TextUtils.isEmpty(etMobile.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity1));
                    return;
                }
                if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity2));
                    return;
                }
                mPresenter.sendCode(etMobile.getText().toString().trim());
            }
        });
        rlPwdSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        rlPayPwdSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPayPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else
                etPayPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        });
        ClickUtil.click(300, btnRegister, new ClickUtil.Click() {
            @Override
            public void click(View view) {
                if (TextUtils.isEmpty(etMobile.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity1));
                    return;
                }
                if (!CheckUtil.isMobile(etMobile.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity2));
                    return;
                }
                if (isShowSMS.equals("1")) {
                    if (TextUtils.isEmpty(etMobileCode.getText())) {
                        ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity5));
                        return;
                    }
                }
                if (TextUtils.isEmpty(etPictureCode.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity6));
                    return;
                }
                String code = codeUtils.getCode();
                if (!code.equalsIgnoreCase(etPictureCode.getText().toString())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity7));
                    bitmap = codeUtils.createBitmap();
                    ivPictureCode.setImageBitmap(bitmap);
                    return;
                }
                if (TextUtils.isEmpty(etPwd.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity8));
                    return;
                }
                if (etPwd.getText().length() < 6) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity9));
                    return;
                }
                if (TextUtils.isEmpty(etPayPwd.getText())) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity10));
                    return;
                }
                if (etPayPwd.getText().length() != 6) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity11));
                    return;
                }
                if (!cbRead.isChecked()) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.appJavaRegisterActivity12));
                    return;
                }
                mPresenter.register(etMobile.getText().toString(),
                        isShowSMS.equals("1") ? etMobileCode.getText().toString() : "",
                        etPwd.getText().toString(),
                        etPayPwd.getText().toString());
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getIsNeedSMS();

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void isShowSMS(RegisterBean registerBean) {
        if (registerBean == null) {
            return;
        }
        if (registerBean.getValue().equals("1")) {
            isShowSMS = "1";
            group_sms.setVisibility(View.VISIBLE);
        }
        if (registerBean.getValue().equals("2")) {
            isShowSMS = "2";
            group_sms.setVisibility(View.GONE);
        }

    }
}
